package com.yjf.ljr.util;

import com.yjf.ljr.util.ImageLoader.ImageCallback;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;



public class PicLoad {
	
	public static ImageLoader imageLoader = new ImageLoader();

	/**
	 * 显示图片
	 * @param imageview
	 * @param imgUrl
	 */
	public static void getImage(final ImageView imageview, String imgTag, String imgUrl, final ProgressBar pb) {
		if (imgUrl != null && imgUrl.length() != 0) {			
			Bitmap bitmap = imageLoader.loadBitmap(imgUrl, imgTag, new ImageCallback() {
				@Override
				public void imageLoader(Bitmap bitmap, String imageUrl) {
					if(bitmap != null) {
						imageview.setImageBitmap(bitmap);
						imageview.setVisibility(View.VISIBLE);
						if (pb != null) {
							pb.setVisibility(View.GONE);
						}
					}
				}
			});
			if(bitmap != null) {
				imageview.setImageBitmap(bitmap);
				imageview.setVisibility(View.VISIBLE);
				imageview.setEnabled(true);
				if (pb != null) {
					pb.setVisibility(View.GONE);
				}
			}
		}
	}
	
	/**
	 * 显示图片
	 * @param imageview
	 * @param imgUrl
	 */
	public static void getImage(final ImageView imageview, String imgTag, String imgUrl, final ProgressBar pb, ImageCallback callBack) {
		if (imgUrl != null && imgUrl.length() != 0) {
			Bitmap bitmap = null;
			if(callBack == null) {
				bitmap = imageLoader.loadBitmap(imgUrl, imgTag, new ImageCallback() {
					@Override
					public void imageLoader(Bitmap bitmap, String imageUrl) {
						if(bitmap != null) {
							imageview.setEnabled(true);
							imageview.setImageBitmap(bitmap);
							imageview.setVisibility(View.VISIBLE);
							if (pb != null) {
								pb.setVisibility(View.GONE);
							}
						}
					}
				});
			} else {
				bitmap = imageLoader.loadBitmap(imgUrl, imgTag, callBack);
			}		
			if(bitmap != null) {				
				imageview.setImageBitmap(bitmap);
				imageview.setVisibility(View.VISIBLE);
				imageview.setEnabled(true);
				if (pb != null) {
					pb.setVisibility(View.GONE);
				}
			}
		}
	}
	
	public static void getImage(final ImageView imageview, String imgTag, String imgUrl) {
		getImage(imageview, imgTag, imgUrl, null);
	}
	
	public static void getBitmap(String imgTag, String imgUrl,final BitmapListener bitmapListener) {
		if (imgUrl != null && imgUrl.length() != 0) {			
			Bitmap bitmap = imageLoader.loadBitmap(imgUrl, imgTag, new ImageCallback() {
				@Override
				public void imageLoader(Bitmap bitmap, String imageUrl) {
					bitmapListener.onBitmapChange(bitmap);
				}
			});
			if(null != bitmap){
				bitmapListener.onBitmapChange(bitmap);
			}	
		}
	}
	
	public static void getRoundedImage(final ImageView imageview, String imgTag, String imgUrl,
			final BitmapListener bitmapListener) {
		if (imgUrl != null && imgUrl.length() != 0) {			
			Bitmap bitmap = imageLoader.loadBitmap(imgUrl, imgTag, new ImageCallback() {
				@Override
				public void imageLoader(Bitmap bitmap, String imageUrl) {
					if(null != bitmap){ 
						if(null != bitmapListener) {
							bitmapListener.onBitmapChange(bitmap);
						}
						bitmap = getRoundedCornerBitmap(bitmap);
						imageview.setImageBitmap(bitmap);
					}
				}
			});
			if(null != bitmap){
				if(null != bitmapListener) {
					bitmapListener.onBitmapChange(bitmap);
				}
				bitmap = getRoundedCornerBitmap(bitmap);
				imageview.setImageBitmap(bitmap);
			}
		}
	}
	
	/**
	 * 获取网络图片
	 * @param imageview
	 * @param imgUrl
	 */
	public static Bitmap getBitmap(String imgTag, String imgUrl) {
		Bitmap bitmap = null;
		if (imgUrl != null && imgUrl.length() != 0) {			
			bitmap = imageLoader.loadBitmap(imgUrl, imgTag,
					new ImageCallback() {
				@Override
				public void imageLoader(Bitmap bitmap, String imageUrl) {
					
				}
			});
		}
		return bitmap;
	}
	
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		final float roundPx = bitmap.getWidth() / 2;

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}

	public interface BitmapListener {
		public void onBitmapChange(Bitmap bitmap);
	}
	
	public static void getImage(final TextView textView, String url,final int defaultDrawable) {
		if (url != null && url.length() != 0) {	
			ImageDownLoadUtils.getInstance().download(url, textView, new ImageDownLoadUtils.ImageCallback() {
				
				@Override
				public void imageLoader(Bitmap bitmap, String imageUrl) {
					if(bitmap != null) {
						textView.setBackgroundDrawable(new BitmapDrawable(bitmap));
					} else {
						textView.setBackgroundResource(defaultDrawable);
					}
				}
			});
		}
	}
	
	public static void getImage(final TextView textView, String url) {
		if (url != null && url.length() != 0) {	
			ImageDownLoadUtils.getInstance().download(url, textView, new ImageDownLoadUtils.ImageCallback() {
				
				@Override
				public void imageLoader(Bitmap bitmap, String imageUrl) {
					if(bitmap != null) {
						textView.setBackgroundDrawable(new BitmapDrawable(bitmap));
					}
				}
			});
		}
	}
	
	public static void getImage(final ImageView image, String url) {
		if(!TextUtils.isEmpty(url)) {
            ImageDownLoadUtils.getInstance().download(url, image, new ImageDownLoadUtils.ImageCallback() {
				
				@Override
				public void imageLoader(Bitmap bitmap, String imageUrl) {
					if(bitmap != null) {
						image.setImageBitmap(bitmap);						
						image.setVisibility(View.VISIBLE);
					}
					
				}
			});
		}
	}

}